<template>
  <!-- 客户管理 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="add"
          v-auth-button="['add/customerManagement']"
        ></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.search"
            placeholder="客户名称"
            clearable
          ></el-input>
          <el-select
            v-model="searchValue.status"
            placeholder="请选择客户状态"
            style="width: 100%"
            clearable
          >
            <el-option label="待审核" :value="0" />
            <el-option label="已通过" :value="1" />
            <el-option label="未通过" :value="2" />
            <el-option label="审核中" :value="3" />
            <!-- <el-option label="作废" :value="4" /> -->
          </el-select>
          <el-select
            v-if="userList.length != 0"
            v-model="searchValue.applicationId"
            placeholder="请选择业务员"
            style="width: 100%"
            clearable
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
        dataType="listSortBydelete"
        stripe
        @filterType="filterType"
        @row-dblclick="handleRowDblclick"
      >
        <el-table-column
          fixed
          label="ID"
          prop="id"
          width="60"
        ></el-table-column>
        <el-table-column
          fixed
          label="客户名称"
          prop="customerName"
          min-width="300"
        ></el-table-column>
        <el-table-column label="客户类型" prop="customertypeName" width="100">
          <template #default="scope">
            <!-- 1普通客户  2重要客户  -->
            <p v-if="scope.row.customertype == 1">普通客户</p>
            <p v-if="scope.row.customertype == 2">重要客户</p>
          </template>
        </el-table-column>
        <el-table-column label="联系人" prop="firstcontacter" width="100">
        </el-table-column>
        <el-table-column
          label="联系人号码"
          prop="phone"
          width="120"
        ></el-table-column>
        <el-table-column
          label="客户号码"
          prop="telephone"
          width="100"
        ></el-table-column>
        <!-- <el-table-column
          label="邮箱"
          prop="email"
          width="200"
        ></el-table-column> -->
        <el-table-column
          label="地址"
          prop="address"
          min-width="150"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <!-- <el-table-column
          label="支付方式"
          prop="typeName"
          width="150"
        ></el-table-column> -->
        <el-table-column
          label="备注"
          prop="memo"
          min-width="150"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="业务员"
          prop="applicantName"
          fixed="right"
          width="150"
        ></el-table-column>
        <el-table-column
          label="审核状态"
          prop="status"
          width="100"
          sortable
          fixed="right"
        >
          <template #default="scope">
            <!--0 未审核   1 审核通过  2 审核未通过 3审核中-->
            <p v-if="scope.row.status == 0">待审核</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">通过</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">未通过</p>
            <p v-if="scope.row.status == 3" style="color: #ff9b31">审核中</p>
            <p v-if="scope.row.status == 4" style="color: #f56c6c">作废</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="right" width="220">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_apply(scope.row, scope.$index)"
                v-if="
                  scope.row.status != 0 && scope.row.adminId == userInfo.userId
                "
                v-auth-button="['apply/customerManagement']"
                >申请修改</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_assign(scope.row, scope.$index)"
                v-auth-button="['assign/customerManagement']"
                >分配</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_unbind(scope.row, scope.$index)"
                v-auth-button="['unbind/customerManagement']"
                >解绑</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_applyLog(scope.row, scope.$index)"
                v-auth-button="['applyLog/customerManagement']"
                >历史记录</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                v-auth-button="['show/customerManagement']"
                >查看</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-auth-button="['edit/customerManagement']"
                v-if="scope.row.status == 0"
                >编辑</el-button
              >
              <!-- <el-popconfirm
                title="确定要升级为月结吗？"
                @confirm="table_upgrade(scope.row, scope.$index)"
                v-if="scope.row.settlement == 1"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['upgrade/customerManagement']"
                    >升级月结</el-button
                  >
                </template>
              </el-popconfirm> -->
              <!--  -->
              <el-popconfirm
                title="确定作废吗？"
                @confirm="table_scrap(scope.row, scope.$index)"
                v-if="scope.row.status != 0"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['scrap/customerManagement']"
                    >作废</el-button
                  >
                </template>
              </el-popconfirm>
              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
                v-if="scope.row.status == 0"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['delete/customerManagement']"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <!-- 申请日志 -->
  <log-dialog
    v-if="dialog.log"
    ref="logDialog"
    @success="handleSaveSuccess"
    @closed="dialog.log = false"
  ></log-dialog>

  <apply-dialog
    v-if="dialog.apply"
    ref="applyDialog"
    @success="handleSaveSuccess"
    @closed="dialog.apply = false"
  ></apply-dialog>

  <!-- 选择分配的用户 -->
  <select-user ref="selectUser" @setUser="setUser"></select-user>
</template>

<script>
import applyDialog from "@/components/applyDialog"
import logDialog from "./components/logDialog.vue"
import selectUser from "@/components/selectUser"
import { queryTypeByDic } from "@/utils/getData"
export default {
  name: "customerManagement",
  components: {
    selectUser,
    applyDialog,
    logDialog,
  },
  data () {
    return {
      dialog: {
        save: false,
        check: false,
        apply: false,
        log: false,
      },
      apiObj: this.$API.customer.getCustomers,
      selection: [],
      paymethodType: [],
      userList: [],
      searchValue: {
        search: null,
        status: "",
        applicationId: "",
      },
      rowData: null,
      userInfo: {},
    }
  },
  mounted () {
    this.userInfo = this.$TOOL.data.get("USER_INFO")
    this.getUserList()
  },
  methods: {
    // 申请修改  -- 重新审核
    table_apply (row) {
      this.dialog.apply = true
      this.$nextTick(() => {
        this.$refs.applyDialog.open(row, 10)
      })
    },

    //添加
    add () {
      this.$router.push({
        path: "/customer/customerManagement/customerAdd",
        query: {
          mode: "add",
        },
      })
    },
    //编辑
    table_edit (row) {
      this.$router.push({
        path: "/customer/customerManagement/customerAdd",
        query: {
          mode: "edit",
          id: row.id,
        },
      })
    },

    // 分配客户
    table_assign (row) {
      this.$refs.selectUser.open(1)
      this.rowData = row
    },
    // 解绑客户、
    async table_unbind (row) {
      let res = await this.$API.customer.unbindCustomer.post({ id: row.id })
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("操作成功")
      } else {
        this.$alert(res.message, "提示", { type: "error" })
      }
    },

    async setUser (val) {
      let data = {
        adminId: val.id,
        id: this.rowData.id,
      }
      let res = await this.$API.customer.assignCustomer.post(data)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("操作成功")
      } else {
        this.$alert(res.message, "提示", { type: "error" })
      }
    },

    // 历史记录（申请修改日志）
    table_applyLog (row) {
      this.dialog.log = true
      this.$nextTick(() => {
        this.$refs.logDialog.open(row)
      })
    },

    //查看
    table_show (row) {
      this.$router.push({
        path: "/customer/customerManagement/customerDetail",
        query: {
          id: row.id,
        },
      })
    },

    // 升级为月结客户
    async table_upgrade (row) {
      var res = await this.$API.customer.upgrade.post({ id: row.id })
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("审计成功")
      }
    },

    // 作废
    table_scrap (row) {
      this.table_del(row)
    },

    //删除
    async table_del (row) {
      var reqData = { id: row.id }
      var res = await this.$API.customer.deleteCustomer.post(reqData)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("操作成功")
      } else {
        this.$alert(res.message, "提示", { type: "error" })
      }
    },

    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    // 获取所有用户列表
    async getUserList () {
      let res = await this.$API.user.getAdminList.get()
      this.userList = res.data
    },

    //本地更新数据
    handleSaveSuccess (data, mode) {
      if (mode == "add") {
        this.$refs.table.refresh()
      } else if (mode == "edit") {
        this.$refs.table.refresh()
      }
    },
    // 处理支付方式
    filterType (data) {
      let tableData = data
      queryTypeByDic("payment").then((res) => {
        let list = res
        tableData.forEach((item) => {
          list.forEach((t_item) => {
            if (item.paymethod == t_item.value) {
              item.typeName = t_item.name
            }
          })
        })
      })

      queryTypeByDic("customerType").then((res) => {
        let list = res
        tableData.forEach((item) => {
          list.forEach((t_item) => {
            if (item.customertype == t_item.value) {
              item.customertypeName = t_item.name
            }
          })
        })
      })
    },
    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    },
  },
};
</script>

<style>
/* .cellClass {
  cursor: pointer;
} */
</style>
